//
//  ST_RecommendPecHeaderView.swift
//  btvideo
//
//  Created by mac on 2020/6/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift


class ST_RecommendPecHeaderView: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        ST_RecommendVideoDataAdapter.shared.index_delegate = self
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    
    fileprivate var transition:ST_RecommendPecVideoTransition?
    
    
    fileprivate let collection_view_height = (118-15).cgFloat
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
        let controlLayout = UICollectionViewFlowLayout()
        controlLayout.scrollDirection = .horizontal
        controlLayout.itemSize = CGSize.init(width: 66, height: collection_view_height)
        controlLayout.minimumInteritemSpacing = 10
        controlLayout.minimumLineSpacing = 10
        controlLayout.sectionInset = UIEdgeInsets.init(top: 15, left: 10, bottom: 0, right: 10)
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self
        control.register(cellWithClass: ST_RecommendPecHeaderChildCell.self)
        control.register(cellWithClass: ST_RecommendPecHeaderChildAddCell.self)
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.backgroundColor = Color.white
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        //        control.backgroundColor = Color.random
        
//       control.alwaysBounceVertical = true
        
        return control
    }()
    
    
   
}


extension ST_RecommendPecHeaderView: ST_PinterestHomeDataAdapterViewDelegate {
    
    func reload_view_from_data_adapter(currentCount:Int){
        
        self.collectionView.reloadData {
        }
        
    }
    
    func fail_load_data(code:Int,error:String){
        
        
        
    }
    
}

//MARK:- UICollectionViewDelegate
extension ST_RecommendPecHeaderView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ST_RecommendVideoDataAdapter.shared.data_array.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.row == 0{

            return collectionView.dequeueReusableCell(withClass: ST_RecommendPecHeaderChildAddCell.self, for: indexPath)
        }
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_RecommendPecHeaderChildCell.self, for: indexPath)
        
        cell.child_model = ST_RecommendVideoDataAdapter.shared.data_array[safe:indexPath.row-1]
        
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.row == 0{
            
            push_to_push_peculiarity_controller(navigationController: self.parentViewController?.navigationController)
            
            return
        }
        
        
        guard let cell = collectionView.cellForItem(at: indexPath) as? ST_RecommendPecHeaderChildCell,let model = cell.child_model,let url = model.imageUrl.compress_image().url else { return }
        
        let startFrame = collectionView.convert(cell.frame, to: nil)
        
        
        self.transition = ST_RecommendPecVideoTransition.init(startFrame: startFrame,isFullScreen: model.height > model.width , url: url)
        
        let toController = ST_RecommendPecVideoController()
        
        ST_RecommendVideoDataAdapter.shared.curr_selected_index = indexPath.row - 1
        
//        toController.normalIdx = indexPath.row
        
        toController.transitioningDelegate = self.transition
        
        toController.modalPresentationStyle = .fullScreen
        
        self.parentViewController?.present(toController, animated: true, completion: nil)
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        
        if scrollView.contentSize.width - ScreenWidth - scrollView.contentOffset.x < 66*2+70{
            
            ST_RecommendVideoDataAdapter.shared.load_data_from_remote()
            
        }
        
    }
    
}

//MARK:- Child Cell

class ST_RecommendPecHeaderChildCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(cover_container_view)
        cover_container_view.addSubview(cover_image_view)
        contentView.addSubview(titleLabel)
        
        
        cover_container_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(66)
        }
        
        cover_image_view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(58)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view.snp.bottom).offset(5)
            make.centerX.equalTo(cover_image_view)
            make.left.right.equalToSuperview()
        }

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 58/2.0
        return control
    }()
    
    
    fileprivate lazy var cover_container_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 66/2.0
        control.borderColor = UIColor.init(hex: 0xFEC207)
        control.borderWidth = 2
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        //        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    
    //    MARK:-
    var child_model:ST_ShowTraitCellModel?
    {
        didSet{
            
            guard let m = child_model else { return  }
            
            
            cover_image_view.pin_setImage(from: m.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = m.nickname
            
        }
    }
    
    
    
}





class ST_RecommendPecHeaderChildAddCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(cover_container_view)
        cover_container_view.addSubview(cover_image_view)
        cover_container_view.addSubview(right_bottom_image_view)
        contentView.addSubview(titleLabel)
        
        
        cover_container_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(66)
        }
        
        cover_image_view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(58)
        }
        
        right_bottom_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.right.bottom.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view.snp.bottom).offset(5)
            make.centerX.equalTo(cover_image_view)
            make.left.right.equalToSuperview()
        }

        NotificationCenter.default.addObserver(self, selector: #selector(refresh_avatar_handle), name: Notification.Name.avatarUpdated, object: nil)
        
        refresh_avatar_handle()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc fileprivate func refresh_avatar_handle(){
        
        if let url = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.avatar.compress_small_image(width: 58*2).url{
        
            cover_image_view.pin_setImage(from: url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
        }
    }
    
    //    MARK: - Properties
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 58/2.0
        return control
    }()
    
    
    fileprivate lazy var right_bottom_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("首页_icon_添加_white"))
        return control
    }()
    
    
    fileprivate lazy var cover_container_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "视频特质"
        control.textColor = Color.init(hexString: "#333333")
        //        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    
    //    MARK:-
    var child_model:ST_ShowTraitCellModel?
    {
        didSet{
            
            guard let m = child_model else { return  }
            
            
            cover_image_view.pin_setImage(from: m.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = m.nickname
            
        }
    }
    
    
    
}
